<template>
  <a-layout class="frontend-layout">
    <!-- 顶部导航 -->
    <a-layout-header class="header">
      <div class="header-content">
        <div class="logo" @click="goToHome">
          <span class="logo-icon">💻</span>
          <span class="logo-text neon-text">Code Share</span>
        </div>
        <div class="header-right">
          <a-button type="link" @click="goToAdmin" v-if="userStore.isAdmin()">
            后台管理
          </a-button>
          <a-button type="link" @click="handleLogout" v-if="userStore.isLogin">
            退出登录
          </a-button>
          <a-button type="link" @click="goToLogin" v-else> 登录 </a-button>
        </div>
      </div>
    </a-layout-header>

    <!-- 主体内容 -->
    <a-layout-content class="content">
      <router-view />
    </a-layout-content>

    <!-- 底部 -->
    <a-layout-footer class="footer">
      <div class="footer-content">
        <p>Code Share © 2025 - 分享你的代码，分享你的知识</p>
      </div>
    </a-layout-footer>
  </a-layout>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { message } from 'ant-design-vue'

const router = useRouter()
const userStore = useUserStore()

// 尝试获取用户信息(如果已登录)
onMounted(() => {
  if (!userStore.isLogin) {
    userStore.fetchUserInfo().catch(() => {
      // 忽略错误,用户未登录也可以访问前台
    })
  }
})

const goToHome = () => {
  // 跳转到首页并清空搜索条件
  router.push('/')
}

const goToAdmin = () => {
  router.push('/admin')
}

const goToLogin = () => {
  router.push('/login')
}

const handleLogout = async () => {
  await userStore.logout()
  message.success('退出登录成功')
  router.push('/')
}
</script>

<style scoped>
.frontend-layout {
  min-height: 100vh;
  background: #0a0e27;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: linear-gradient(135deg, #1a1f3a 0%, #0f1229 100%);
  border-bottom: 1px solid rgba(74, 158, 255, 0.2);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  padding: 0;
  height: 64px;
  line-height: 64px;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}

.logo-icon {
  font-size: 28px;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  background: linear-gradient(135deg, #4a9eff 0%, #6bb3ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  gap: 16px;
}

.content {
  margin-top: 64px;
  min-height: calc(100vh - 64px - 70px);
  padding: 24px;
}

.footer {
  background: #1a1f3a;
  border-top: 1px solid rgba(74, 158, 255, 0.2);
  text-align: center;
  padding: 24px 0;
}

.footer-content p {
  margin: 0;
  color: #8a8fa8;
}
</style>

